<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Good
 * 
 * @property int $id
 * @property int $cat_id
 * @property int $branch_id
 * @property string $images
 * @property string $goods_name
 * @property string|null $goods_description
 * @property int $is_single
 * @property bool $status
 * @property int $sales
 * @property int $browse
 * @property int $collect
 * @property int $sort
 * @property bool $is_new
 * @property bool|null $is_hot
 * @property bool|null $is_kill
 * @property int $sku_id
 * @property Carbon|null $create_at
 * @property Carbon|null $update_at
 * @property Carbon|null $delete_at
 *
 * @package App\Models
 */
class Good extends BaseModels
{

    use SoftDeletes;

    const SINGLE_MORE = 2; //多规格

    const SINGLE = 1; //单规格

    const ON_SALE = 1; //已上架

    const UN_SALE = 0; //未上架

	protected $table = 'zh_goods';

	protected $casts = [
		'cat_id' => 'int',
		'branch_id' => 'int',
		'sales' => 'int',
		'browse' => 'int',
		'collect' => 'int',
		'sort' => 'int',
	];

    const LIST_FIELD = [
        'id', 'images', 'goods_name', 'is_new', 'is_hot', 'cat_id', 'branch_id', 'sales'
    ];


	protected $fillable = [
		'cat_id',
		'branch_id',
        'images',
		'goods_name',
		'goods_description',
		'is_single',
		'status',
		'sales',
		'browse',
		'collect',
		'sort',
		'is_new',
		'is_hot',
		'is_kill',
        'sku_id',
		'create_at',
		'update_at',
		'delete_at',
	];


	protected $appends = [
	    'branch_name', 'cat_name', 'first_cat_id'
    ];


    /**
     * @return mixed
     */
	public function getBranchNameAttribute()
    {
        return GoodsBranch::whereKey($this->branch_id)->value('branch_name');
    }


    /**
     * @return mixed
     */
    public function getCatNameAttribute()
    {
        return GoodsCategory::whereKey($this->cat_id)->value('category_name');
    }


    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne|\App\Models\GoodsCategory
     */
    public function category()
    {
        return $this->hasOne(GoodsCategory::class,'id','cat_id');
    }

    /**
     * @return mixed
     */
    public function getFirstCatIdAttribute()
    {
        return GoodsCategory::whereKey(GoodsCategory::find($this->cat_id)->pid ?? 0)->value('id');
    }
}
